#!/bin/sh

########################################################################
# This test checks the basic operations with the KEDR module.
# Usage: 
#   sh ins_rm_with_target.sh <target_before_kedr>
# If <target_before_kedr> is specified and is non-zero, the target
# module will be loaded before the kedr module to check if the 
# latter reports error as it should.
########################################################################

########################################################################
# A function to check prerequisites: whether the necessary files exist,
# etc.
########################################################################
checkPrereqs()
{
    # 'test -f' cannot check existing of module if it is given by name.
    #if test ! -f "${KEDR_MODULE}"; then
    #    printf "KEDR core module is missing: ${KEDR_MODULE}\n"
    #    exit 1
    #fi
    
    if test ! -f "${PAYLOAD_MODULE}"; then
        printf "Payload module is missing: ${PAYLOAD_MODULE}\n"
        exit 1
    fi
}

########################################################################
# Cleanup function
########################################################################
cleanupAll()
{
    @LSMOD@ | grep "${TARGET_NAME}" > /dev/null 2>&1
    if test $? -eq 0; then
        sh ${TARGET_DIR}/kedr_sample_target unload
    fi
    
    @LSMOD@ | grep "${PAYLOAD_NAME}" > /dev/null 2>&1
    if test $? -eq 0; then
        @RMMOD@ "${PAYLOAD_NAME}"
    fi
    
    @LSMOD@ | grep "@KEDR_CORE_NAME@" > /dev/null 2>&1
    if test $? -eq 0; then
        @RMMOD@ "@KEDR_CORE_NAME@"
    fi
}
########################################################################
# Scenario:
#   load KEDR core, payload module
#   load target
#   try to unload core (must fail)
#   write some data to the device created by the target
#   unload target
#   unload the payload module
#   unload core (must succeed this time)
########################################################################
checkCommonScenario()
{
    printf "Executing checkCommonScenario()\n"
    
    @KEDR_CORE_LOAD_COMMAND@ target_name="${TARGET_NAME}" || exit 1

    @INSMOD@ "${PAYLOAD_MODULE}"
    if test $? -ne 0; then
        printf "Failed to load the payload module\n"
        cleanupAll
        exit 1
    fi

    sh ${TARGET_DIR}/kedr_sample_target load
    if test $? -ne 0; then
        printf "Failed to load the target module: ${TARGET_NAME}.ko\n"
        cleanupAll
        exit 1
    fi

    printf "Sending some data to the device created by the target\n"
    echo "abracadabra123456789" > ${KEDR_TEST_DEVICE}
    if test $? -ne 0; then
        printf "Errors occured while trying to write to ${KEDR_TEST_DEVICE}\n"
    # A kind of a non-fatal assertion.
    fi

    printf "Trying to unload the KEDR core while the target is under analysis (must fail)\n"
    @RMMOD@ "@KEDR_CORE_NAME@"
    if test $? -eq 0; then
        printf "The KEDR core module must have been locked "
        printf "as long as the target module is under analysis\n"
        cleanupAll
        exit 1
    fi
    printf "This error was expected, continuing.\n"

    sh ${TARGET_DIR}/kedr_sample_target unload
    if test $? -ne 0; then
        printf "Failed to unload the target module: ${TARGET_NAME}.ko\n"
        cleanupAll
        exit 1
    fi
    cd "${WORK_DIR}"

    @RMMOD@ "${PAYLOAD_NAME}"
    if test $? -ne 0; then
        printf "Failed to unload the payload module: ${PAYLOAD_NAME}\n"
        cleanupAll
        exit 1
    fi

    @RMMOD@ "@KEDR_CORE_NAME@"
    if test $? -ne 0; then
        printf "Failed to unload the KEDR core module\n"
        cleanupAll
        exit 1
    fi
}

########################################################################
# Scenario:
#   load target
#   try to load KEDR core (must fail)
#   unload target
########################################################################
checkTargetFirstScenario()
{
    printf "Executing checkTargetFirstScenario()\n"
    
    sh ${TARGET_DIR}/kedr_sample_target load
    if test $? -ne 0; then
        printf "Failed to load the target module: ${TARGET_NAME}.ko\n"
        cleanupAll
        exit 1
    fi

    printf "Trying to load the KEDR core when the target is already loaded "
    printf "(must fail)\n"
    @KEDR_CORE_LOAD_COMMAND@ target_name="${TARGET_NAME}"
    if test $? -eq 0; then
        printf "KEDR core must have failed to load if the target "
        printf "was already loaded at that moment\n"
        cleanupAll
        exit 1
    fi
    printf "This error was expected, continuing.\n"

    sh ${TARGET_DIR}/kedr_sample_target unload
    if test $? -ne 0; then
        printf "Failed to unload the target module: ${TARGET_NAME}.ko\n"
        cleanupAll
        exit 1
    fi
    cd "${WORK_DIR}"
}

########################################################################
# main
########################################################################
WORK_DIR=${PWD}
KEDR_TEST_DEVICE=/dev/cfake0

if test $# -ne 1; then
    printf "Usage: sh ins_rm_with_target.sh <target_before_kedr>\n"
    exit 1
fi

TARGET_NAME="@TARGET_NAME@"
TARGET_DIR="@TEST_MODULES_DIR@/sample_target"

PAYLOAD_NAME="simple_payload"
PAYLOAD_MODULE="${PAYLOAD_NAME}/${PAYLOAD_NAME}.ko"

checkPrereqs

printf "KEDR core module: @KEDR_CORE_NAME@\n"
printf "Target module: ${TARGET_DIR}/${TARGET_NAME}.ko\n"
printf "Payload module: ${PAYLOAD_MODULE}\n"

# Select the test scenario
TARGET_FIRST=$1

if test "t${TARGET_FIRST}" = "t"; then
    checkCommonScenario
else 
    if test ${TARGET_FIRST} -eq 0; then 
        checkCommonScenario
    else
        checkTargetFirstScenario
    fi
fi

# just in case
cleanupAll

# test passed
exit 0
